package com.tbit.main.pojo.maintain_performance.group;

import com.tbit.main.constant.enums.ConditionLogic;
import com.tbit.main.constant.enums.PerformanceBizType;
import com.tbit.main.constant.enums.PerformanceRuleType;
import com.tbit.main.pojo.PerformanceRule;
import com.tbit.main.pojo.PerformanceRuleCondition;
import com.tbit.main.pojo.PerformanceRuleGroup;
import com.tbit.main.pojo.maintain_performance.Range;
import com.tbit.main.pojo.maintain_performance.BizRuleGroup;
import com.tbit.main.pojo.maintain_performance.rule.IntervalBasedRule;
import lombok.Data;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 换电产生订单时段配置
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/18
 */
@Data
public class IntervalBasedGroup implements BizRuleGroup {
    /**
     * 是否启用
     */
    private Boolean enabled;
    /**
     * 任务完成时间范围
     */
    private List<Range<LocalTime>> completedDuration;
    /**
     * 24小时内最多奖励订单数
     */
    private Integer maxOrderCount;
    /**
     * 时段对应绩效配置
     */
    private List<IntervalBasedRule> subRules;

    @Override
    public PerformanceRuleGroup toRaw(PerformanceBizType bizType, Integer accountId) {
        PerformanceRuleGroup group = new PerformanceRuleGroup();
        group.setAccountId(accountId);
        group.setBizType(bizType);
        group.setRuleType(PerformanceRuleType.INTERVAL);
        group.setEnabled(enabled);
        group.setRules(new ArrayList<>());

        if (subRules == null) {
            return group;
        }
        for (IntervalBasedRule rule : subRules) {
            PerformanceRule performanceRule = new PerformanceRule();
            performanceRule.setPerformance(rule.getPerformance());
            performanceRule.setConditionLogic(ConditionLogic.AND);
            performanceRule.setConditions(new ArrayList<>());

            // 任务完成时间条件
            performanceRule.getConditions().add(Range.combine("completedTime", completedDuration, ConditionLogic.OR));

            // 产生订单时段条件
            performanceRule.getConditions().add(rule.getOrderInterval().toCondition("orderTime"));

            group.getRules().add(performanceRule);
        }

        Map<String, Object> metaInfo = new HashMap<>();
        metaInfo.put("maxOrderCount", maxOrderCount);
        group.setMetaInfo(metaInfo);

        return group;
    }

    @Override
    public void initFromRaw(PerformanceRuleGroup group) {
        setEnabled(group.getEnabled());
        setSubRules(new ArrayList<>());
        for (PerformanceRule rule : group.getRules()) {
            IntervalBasedRule subRule = new IntervalBasedRule();
            subRule.setPerformance(rule.getPerformance());
            for (PerformanceRuleCondition condition : rule.getConditions()) {
                switch (condition.getKey()) {
                    case "completedTime":
                        if (getCompletedDuration() == null) {
                            setCompletedDuration(Range.unCombine(condition, LocalTime::parse));
                        }
                        break;
                    case "orderTime":
                        subRule.setOrderInterval(Range.from(condition, LocalTime::parse));
                        break;
                }
            }
            getSubRules().add(subRule);
        }

        if (group.getMetaInfo() != null) {
            setMaxOrderCount((Integer) group.getMetaInfo().get("maxOrderCount"));
        }
    }
}
